package com.example.myproject.server;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.example.myproject.client.PersistentService;
import com.example.myproject.client.entities.Note;
import com.example.myproject.shared.TimeException;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;
@SuppressWarnings("serial")
public class PersistentServiceImpl extends RemoteServiceServlet implements
PersistentService {
@Override
public void persistNote(Note a) throws IllegalArgumentException,
TimeException {
checkTime();
a.setParent(a.getParent().toLowerCase());
String ip = this.getThreadLocalRequest().getRemoteAddr();
// The Objectify service for the entity must be registered before any
// operations can be executed
ObjectifyService.register(Note.class);
Objectify ofy = ObjectifyService.begin();
Note note = a;
// Use setters to populate the object
// the Key will be auto generated and does not need to be set
ofy.put(note);
}
public ArrayList<Note> searchNote(String searchString) throws TimeException {
checkTime();
// System.out.println(seconds);
// System.out.println("The number of sessions is: "
// + SessionListener.getActiveSessions());
searchString = searchString.toLowerCase();
ObjectifyService.register(Note.class);
Objectify ofy = ObjectifyService.begin();
Query<Note> q = ofy.query(Note.class).filter("parent", searchString)
.order("-date");
ArrayList<Note> notes = new ArrayList<Note>();
// Loop the query results and add to the array
for (Note fetched : q) {
notes.add(fetched);
}
return notes;
}
private void checkTime() throws TimeException {
HttpSession session = this.getThreadLocalRequest().getSession(true);
if (session.getAttribute("timeA") == null) {
session.setAttribute("timeA", new Date().getTime());
} else {
long timeA = (Long) session.getAttribute("timeA");
long timeB = new Date().getTime();
int timeElapsed = 10 - (int) ((timeB - timeA) / 1000);
if (timeElapsed <= 0) {
// System.out.println("Start");
session.setAttribute("timeA", new Date().getTime());
} else {
throw new TimeException(timeElapsed);
// System.out.println("Wait for " + timeToWait + " seconds.");
}
}
}
@Override
public String debugInfo(String[] data) {
// checkTime();
StringBuilder result = new StringBuilder();
HttpServletRequest request = this.getThreadLocalRequest();
result.append("<pre>");
result.append(request.toString());
result.append("Remote Address: " + request.getRemoteAddr());
result.append("</pre>");
return result.toString();
}
// temp fix for header X-GWT-PERMUTATION is not added randomly by the RPC
// client proxy
@Override
protected void checkPermutationStrongName() throws SecurityException {
// Content-Type text/x-gwt-rpc; charset=utf-8
// X-GWT-Permutation F1AEC601C5D8E4490E7096AB58EB
HttpServletRequest req = this.getThreadLocalRequest();
if (!req.getContentType().contains("text/x-gwt-rpc")) {
super.checkPermutationStrongName();
}
}
}